package com.njust.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.njust.common.CommonMethod;
import com.njust.common.Const;
import com.njust.common.ServerRespone;
import com.njust.dataobject.*;
import com.njust.enums.SelectType;
import com.njust.form.FaultForm;
import com.njust.form.ReportForm1;
import com.njust.form.ReportFrom;
import com.njust.mapper.MotorInfoMapper;
import com.njust.mapper.TrainFaultMapper;
import com.njust.mapper.TrainInfoMapper;
import com.njust.mapper.VtInfoMapper;
import com.njust.service.HistoryService;
import com.njust.service.SysParamService;
import com.njust.utils.*;
import com.njust.vo.HistoryVO.HistoryMotorTempVo;
import com.njust.vo.HistoryVO.HistoryReportVO;
import com.njust.vo.HistoryVO.HistoryVo;
import com.njust.vo.HistoryVO.OptiAndAvgVO;
import com.njust.vo.PageVO.PageResultVo;
import com.njust.vo.TrainParamVO.TrainParamVO;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.sql.Select;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.*;

/**
 * @author zjwpro@foxmail.com
 * @date 2018/3/8 下午9:48
 */
@Service
@Slf4j
public class HistoryServiceImpl implements HistoryService {

    @Autowired
    private TrainInfoMapper trainInfoMapper;

    @Autowired
    private VtInfoMapper vtInfoMapper;

    @Autowired
    private MotorInfoMapper motorInfoMapper;

    @Autowired
    private TrainFaultMapper trainFaultMapper;

    @Autowired
    private SysParamService sysParamService;

    /***
     * 根据条件可以分为三块
     * 这个方法还缺少系统的分页 具体的分页信息 要看前台 暂定20页
     * @param reportFrom
     * 此处需要添加峭度值，以及峭度值的状态todo
     * 且只有在查找振动数据的时候才会需要峭度值以及峭度状态
     * @return
     */
    @Override
    public ServerRespone reportQuery(ReportFrom reportFrom) throws ParseException {
        //返回的对象
        List<HistoryReportVO> historyReportVOS = new ArrayList<>();


        //根据车号和时间选出具体的train_onlyid
        String trainId = reportFrom.getTrainId();
        List<Long> trainOnlyIdlist  = new ArrayList<>();
        if(trainId.equals("0")){
            //表明进行全列车号的查询
            List<String> trainIds = Arrays.asList(Const.TRAINID);
            trainOnlyIdlist = trainInfoMapper.findTrainOnlyIdBytrainIdInAndtrainDate(trainIds,
                    DateUtil.String2Date(reportFrom
                            .getStartTime()), DateUtil.String2Date(reportFrom.getEndTime()));

        }else {
            trainOnlyIdlist = trainInfoMapper.findTrainOnlyIdBytrainIdAndtrainDate(trainId,
                    DateUtil.String2Date(reportFrom
                            .getStartTime()), DateUtil.String2Date(reportFrom.getEndTime()));
        }
        if (trainOnlyIdlist.size() == 0) {
            return ServerRespone.createByErrorMessage("无数据");
        }


        ServerRespone sr = assembleDataBySelectType(historyReportVOS, trainOnlyIdlist,
                reportFrom.getSelectType(), reportFrom
                        .getCarriageNum(), reportFrom.getWheelNum(), reportFrom.getPage(),
                reportFrom.getPageSize(), reportFrom.getMotorNum());
        return sr;
    }

    //报警列表查询,过滤掉正常的数据,方法已经写，测试是否正常
    @Override
    public ServerRespone faultQuery(FaultForm faultForm) throws ParseException {
        //返回的对象
        List<HistoryReportVO> historyReportVOS = new ArrayList<>();
        //符合要求的车号
        List<Long> trainOnlyIdlist=new ArrayList<>();
        TrainInfoExample trainInfoExample=new TrainInfoExample();
        TrainInfoExample.Criteria criteria = trainInfoExample.createCriteria();
        criteria.andTrainDateBetween(DateUtil.String2Date(faultForm
                .getStartTime()), DateUtil.String2Date(faultForm.getEndTime()));
        List<TrainInfo> trainInfos ;
        if(faultForm.getTrainId().equals("0")){
            List<String> trainIds = Arrays.asList(Const.TRAINID);
            criteria.andTrainStateIn(faultForm.getFaultType());
            criteria.andTrainIdIn(trainIds);
            //获取trainInfo
            trainInfos = trainInfoMapper.selectByExample(trainInfoExample);

        }else {
            //根据单个车号进行数据查询
            criteria.andTrainStateIn(faultForm.getFaultType());
            criteria.andTrainIdEqualTo(faultForm.getTrainId());
            trainInfos = trainInfoMapper.selectByExample(trainInfoExample);
        }
        if(trainInfos.size()==0){
            return ServerRespone.createByErrorMessage("无数据");
        }
        for (TrainInfo trainInfo:trainInfos){
            trainOnlyIdlist.add(trainInfo.getTrainOnlyid());
        }
        //查询出所有故障的 trainOnlyId
        List<Long> faultTrainOnlyIdList = trainFaultMapper
                .findTrainOnlyIdByTrainOnlyIdAndFaultTypeAndFaultRank
                        (trainOnlyIdlist, faultForm.getSelectType(), faultForm.getFaultType());
        if (faultTrainOnlyIdList.size() == 0) {
            return ServerRespone.createByErrorMessage("无数据");
        }
        //表明进行故障信息的查询,最后一次修改,暂时还没有进行更换
   if(faultForm.getFaultType().size()>0){
            ServerRespone serverRespone = assembleDataBySelectType1(historyReportVOS, faultTrainOnlyIdList,
                    faultForm.getSelectType(), faultForm.getCarriageNum(),
                    faultForm.getWheelNum(), faultForm.getPage(), faultForm.getPageSize(),
                    faultForm.getMotorNum(),faultForm.getFaultType());
            return serverRespone;
        }else{
            ServerRespone serverRespone = assembleDataBySelectType(historyReportVOS, faultTrainOnlyIdList,
                    faultForm.getSelectType(), faultForm.getCarriageNum(),
                    faultForm.getWheelNum(), faultForm.getPage(), faultForm.getPageSize(),
                    faultForm.getMotorNum());
            return serverRespone;
            //此处需要显示默认的一个月的信息
        }


    }

//不知道可不可用

    private ServerRespone assembleDataBySelectType1(List<HistoryReportVO> historyReportVOS, List<Long> trainOnlyIdlist, Integer selectType, String carriageNum, Integer wheelNum, Integer page, Integer pageSize, Integer motorNum,List<Integer> faultTypes) {
        //需要判断每条数据的状态，进行正常数据的过滤,烦，
        // 不是应该采集那块确定好是哪一个电机哪一个轮子出问题吗,怎么要我这边进行判断啊
        //首先进行轮子信息的封装
        ServerRespone serverRespone = sysParamService.showParam();
        TrainParamVO trainParam = (TrainParamVO) serverRespone.getData();
        if (trainParam == null) {
            return ServerRespone.createByErrorMessage("请先输入阈值");
        }
        //表明进行整栋相关的数据进行查询
        if (selectType.equals(SelectType.ZHENDONG.getCode()) || selectType.equals(SelectType.ZHOUWEN.getCode())) {
            //表明为振动或者为轴温数据,上面只给出了故障车的trainOnlyId
            VtInfoExample vtInfoExample = new VtInfoExample();
            VtInfoExample.Criteria criteria = vtInfoExample.createCriteria();
            List<VtInfo> vtInfos = new ArrayList<>();
            //需要分为四种情况进行查询,全车厢+全轮，全车厢+单轮子,单车箱+全轮，单车箱+单轮
            //需要将全部的正常信息过滤掉
            //全车箱，全轮
            if ((carriageNum.equals("all")) && (wheelNum.equals(0))) {
                List<String> Carriages = Arrays.asList(Const.CARRIAGES);//全车厢
                List<Integer> Wheels = Arrays.asList(Const.WHEELS);//全轮子
                criteria.andCarriageNumIn(Carriages);
                criteria.andWheelNumIn(Wheels);
                criteria.andTrainOnlyidIn(trainOnlyIdlist);
                vtInfoExample.setOrderByClause("id desc");
                vtInfos = vtInfoMapper.selectByExample(vtInfoExample);
            }
            //单车厢+全轮
            if ((!carriageNum.equals("all")) && (wheelNum.equals(0))) {
                List<Integer> Wheels = Arrays.asList(Const.WHEELS);//全轮子
                criteria.andTrainOnlyidIn(trainOnlyIdlist);
                criteria.andWheelNumIn(Wheels);
                criteria.andCarriageNumEqualTo(carriageNum);
                vtInfoExample.setOrderByClause("id desc");
                vtInfos = vtInfoMapper.selectByExample(vtInfoExample);
            }
            //全车厢+单轮
            if (carriageNum.equals("all") && (!wheelNum.equals(0))) {
                List<String> Carriages = Arrays.asList(Const.CARRIAGES);//全车厢
                criteria.andTrainOnlyidIn(trainOnlyIdlist);
                criteria.andWheelNumEqualTo(wheelNum);
                criteria.andCarriageNumIn(Carriages);
                vtInfoExample.setOrderByClause("id desc");
                vtInfos = vtInfoMapper.selectByExample(vtInfoExample);
            }
            //单车箱+单轮
            if ((!carriageNum.equals("all")) && (!wheelNum.equals(0))) {
                criteria.andTrainOnlyidIn(trainOnlyIdlist);
                criteria.andWheelNumEqualTo(wheelNum);
                criteria.andCarriageNumEqualTo(carriageNum);
                vtInfoExample.setOrderByClause("id desc");
                vtInfos = vtInfoMapper.selectByExample(vtInfoExample);
            }

             if(vtInfos.size()==0){
                return  ServerRespone.createByErrorMessage("无数据");
             }


            //进行信息的筛选,分为振动信息，和轴温信息
            if (selectType.equals(SelectType.ZHOUWEN.getCode())) {
                for (VtInfo vtInfo : vtInfos) {
                    HistoryReportVO historyReportVO = new HistoryReportVO();
                    historyReportVO.setTrainDate(DateUtil.Date2String(vtInfo.getTrainDate()));
                    historyReportVO.setCarriageNum(vtInfo.getCarriageNum());
                    historyReportVO.setWheelNum(vtInfo.getWheelNum());
                    historyReportVO.setZxTemp(vtInfo.getZxTemp());
                    historyReportVO.setZxTempinc(vtInfo.getZxTempinc());
                    TrainInfo trainInfo = trainInfoMapper.selectByPrimaryKey(vtInfo.getTrainOnlyid());
                    historyReportVO.setTrainId(trainInfo.getTrainId());


                    //需要添加颜色
                    historyReportVO.setZxState(CompareUtil.compare(1, trainParam.getZwWarn().doubleValue(), trainParam.getZwAlarm()
                            .doubleValue(), vtInfo.getZxTemp().doubleValue()));
                    historyReportVO.setZxTempIncState(CompareUtil.compare(1, trainParam.getZwincWarn().doubleValue(), trainParam.getZwincAlarm()
                            .doubleValue(), vtInfo.getZxTempinc().doubleValue()));
                    //进行数据的筛选,但要判断其是预警选了还是报警选了，还是两个都选了？？？
                    //需要判断复选框的情况
                    //表明为异常数据,还需要对异常数据进行分类
                    //预警报警,由于historyReportVO有三种状态的数据
                    //表示全为异常数据，解决了一种
                    if (historyReportVO.getZxState() != 0 || historyReportVO.getZxTempIncState() !=0) {
                        //预警数据,确定预警报警的数字要对应上
                           if(faultTypes.contains(1)&&(historyReportVO.getZxState().equals(2)||historyReportVO.getZxTempIncState().equals(2))){
                               historyReportVOS.add(historyReportVO);
                           }
                           if(faultTypes.contains(2)&&(historyReportVO.getZxState().equals(1)||historyReportVO.getZxTempIncState().equals(1))){
                               historyReportVOS.add(historyReportVO);
                           }

                    } else {
                        continue;
                    }
                }
                //数据筛选完，进行手动分页,添加手动分页工具,希望分页工具有效啊
                Paging pagination = Paging.pagination(historyReportVOS.size(), pageSize, page);
                List list = PageUtil.pagingUtil(pagination, historyReportVOS);//查询出当前的结果集，进行结果的封装
                PageResultVo pageResultVo = CommonMethod.packingPage(list, pagination, pageSize, page);
                return ServerRespone.createBySuccess(pageResultVo);

            } else {
                //表明进行振动信息的查询
                for (VtInfo vtInfo : vtInfos) {
                    HistoryReportVO historyReportVO = new HistoryReportVO();
                    BeanUtils.copyProperties(vtInfo, historyReportVO);
                    historyReportVO.setTrainDate(DateUtil.Date2String(vtInfo.getTrainDate()));
                    historyReportVO.setZxTemp(null);//不设置轴温以及轴温的温升
                    historyReportVO.setZxTempinc(null);
                    TrainInfo trainInfo = trainInfoMapper.selectByPrimaryKey(vtInfo.getTrainOnlyid());
                    historyReportVO.setTrainId(trainInfo.getTrainId());
                    //均方根
                    historyReportVO.setJfgState(CompareUtil.compare(1, trainParam.getJfgWarn().doubleValue()
                            , trainParam.getJfgAlarm().doubleValue(), vtInfo.getJfgValue().doubleValue()));

                    //峭度
                    historyReportVO.setQdState(CompareUtil.compare(1, trainParam.getQdWarn().doubleValue()
                            , trainParam.getQdAlarm().doubleValue(), vtInfo.getQdValue().doubleValue()));

                    //峭度因子
                    historyReportVO.setQdyzState(CompareUtil.compare(1, trainParam.getQdyzWarn().doubleValue()
                            , trainParam.getQdyzAlarm().doubleValue(), vtInfo.getQdyzValue().doubleValue()));

                    //峰值
                    historyReportVO.setFzState(CompareUtil.compare(1, trainParam.getFzWarn().doubleValue()
                            , trainParam.getFzyzAlarm().doubleValue(), vtInfo.getFzValue().doubleValue()));

                    //峰值因子
                    historyReportVO.setFzyzState(CompareUtil.compare(1, trainParam.getFzyzWarn().doubleValue()
                            , trainParam.getFzyzAlarm().doubleValue(), vtInfo.getFzyzValue().doubleValue()));
                    if ((historyReportVO.getJfgState() != 0) || (historyReportVO.getFzState() != 0) || (historyReportVO.getFzyzState() != 0) || historyReportVO.getQdState() != 0 || historyReportVO.getQdyzState() != 0) {
                        //表明只存在异常数据，但好像是要分预警数据以及报警数据
                        //需要对数据进行预警报警的区分
                        if(faultTypes.contains(1)&&(historyReportVO.getJfgState() == 2) || (historyReportVO.getFzState() ==2) || (historyReportVO.getFzyzState() ==2) || historyReportVO.getQdState() ==2 || historyReportVO.getQdyzState() ==2){
                            //预警数据
                            historyReportVOS.add(historyReportVO);
                        }
                        if(faultTypes.contains(2)&&((historyReportVO.getJfgState() == 1) || (historyReportVO.getFzState() ==1) || (historyReportVO.getFzyzState() ==1) || historyReportVO.getQdState() ==1 || historyReportVO.getQdyzState() ==1)){
                        historyReportVOS.add(historyReportVO);
                        }
                        }else{
                        continue;
                    }
                }

                Paging pagination = Paging.pagination(historyReportVOS.size(), pageSize, page);
                List list = PageUtil.pagingUtil(pagination, historyReportVOS);
                PageResultVo pageResultVo = CommonMethod.packingPage(list, pagination, pageSize, page);
                return ServerRespone.createBySuccess(pageResultVo);
            }

        }else{
            //表明为电机温度那一块,明天todo  ,进行相应的调整
            MotorInfoExample motorInfoExample = new MotorInfoExample();
            MotorInfoExample.Criteria criteria = motorInfoExample.createCriteria();

            List<MotorInfo> motorInfoList = new ArrayList<>();
            //单车厢+单电机
            if ((!carriageNum.equals("all")) && (!motorNum.equals(0))) {
                criteria.andCarriageNumEqualTo(carriageNum);
                criteria.andMotorNumEqualTo(motorNum);
                //列车唯一编号
                criteria.andTrainOnlyidIn(trainOnlyIdlist);
                motorInfoExample.setOrderByClause("id DESC");
                //查询出所有的数据
                motorInfoList = motorInfoMapper.selectByExample(motorInfoExample);
            }
            //全车厢+单电机
            if ((carriageNum.equals("all")) && (!motorNum.equals(0))) {
                List<String> carriagelist = Arrays.asList(Const.CARRIAGES);
                criteria.andCarriageNumIn(carriagelist);
                criteria.andMotorNumEqualTo(motorNum);
                //列车唯一编号
                criteria.andTrainOnlyidIn(trainOnlyIdlist);
                motorInfoExample.setOrderByClause("id DESC");
                motorInfoList = motorInfoMapper.selectByExample(motorInfoExample);
            }
            //单车厢+全电机
            if ((!carriageNum.equals("all")) && (motorNum.equals(0))) {
                List<Integer> motorList = Arrays.asList(Const.MOTORNUM);
                criteria.andCarriageNumEqualTo(carriageNum);
                criteria.andMotorNumIn(motorList);
                //列车唯一编号
                criteria.andTrainOnlyidIn(trainOnlyIdlist);
                motorInfoExample.setOrderByClause("id DESC");
                motorInfoList = motorInfoMapper.selectByExample(motorInfoExample);
            }
            //双全
            if ((carriageNum.equals("all")) && (motorNum.equals(0))) {
                List<String> carriagelist = Arrays.asList(Const.CARRIAGES);
                List<Integer> motorList = Arrays.asList(Const.MOTORNUM);
                criteria.andCarriageNumIn(carriagelist);
                criteria.andMotorNumIn(motorList);
                //列车唯一编号
                criteria.andTrainOnlyidIn(trainOnlyIdlist);
                motorInfoExample.setOrderByClause("id DESC");
                motorInfoList = motorInfoMapper.selectByExample(motorInfoExample);
            }

            if (motorInfoList.size() == 0) {
                return ServerRespone.createByErrorMessage("无数据");
            }

            //表明为电机温度的查询
            for (MotorInfo motorInfo : motorInfoList) {
                HistoryReportVO historyReportVO = new HistoryReportVO();
                BeanUtils.copyProperties(motorInfo, historyReportVO);
                TrainInfo trainInfo = trainInfoMapper.selectByPrimaryKey(motorInfo.getTrainOnlyid());
                historyReportVO.setTrainId(trainInfo.getTrainId());
                historyReportVO.setTrainDate(DateUtil.Date2String(motorInfo.getTrainDate()));
                historyReportVO.setMotorTempState(CompareUtil.compare(1, trainParam.getMotortempWarn().doubleValue()
                        , trainParam.getMotortempAlarm().doubleValue(), motorInfo.getMotorTemp().doubleValue()));
                historyReportVO.setMotorTempIncState(CompareUtil.compare(1, trainParam.getMotortempincWarn().doubleValue()
                        , trainParam.getMotortempincAlarm().doubleValue(), motorInfo.getMotorTempinc().doubleValue()));

               //需要进行数据的封装,过滤掉正常的数据
                if(historyReportVO.getMotorTempState()!=0||historyReportVO.getMotorTempIncState()!=0){
                    //为异常数据
                    if(faultTypes.contains(1)&&(historyReportVO.getMotorTempIncState().equals(2)||historyReportVO.getMotorTempState().equals(2))){
                        //表明为预警数据
                        historyReportVOS.add(historyReportVO);
                    }

                    if(faultTypes.contains(2)&&(historyReportVO.getMotorTempState().equals(1)||historyReportVO.getMotorTempIncState().equals(1))){
                        if(historyReportVOS.contains(historyReportVO)){
                            continue;
                        }else{
                            historyReportVOS.add(historyReportVO);
                        }
                    }
                }
            }
            Paging pagination = Paging.pagination(historyReportVOS.size(), pageSize, page);
            List list = PageUtil.pagingUtil(pagination, historyReportVOS);
            PageResultVo pageResultVo = CommonMethod.packingPage(list, pagination, pageSize, page);
            return ServerRespone.createBySuccess(pageResultVo);
        }
    }



    private ServerRespone assembleDataBySelectType(List<HistoryReportVO> historyReportVOS, List<Long> trainOnlyIdlist, Integer selectType, String carriageNum, Integer wheelNum, Integer page, Integer pageSize, Integer motorNum) {
        ServerRespone serverRespone = sysParamService.showParam();
        TrainParamVO trainParam = (TrainParamVO) serverRespone.getData();
        if (trainParam == null) {
            return ServerRespone.createByErrorMessage("请先输入阈值");
        }
        if (selectType.equals(SelectType.ZHENDONG.getCode()) || selectType.equals(SelectType.ZHOUWEN.getCode())) {

            VtInfoExample vtInfoExample = new VtInfoExample();
            VtInfoExample.Criteria criteria = vtInfoExample.createCriteria();
            List<VtInfo> vtInfos = new ArrayList<>();
            PageResultVo pageResultVo = new PageResultVo();
            /**
             * 分为三种情况 车厢全+单轮 单车厢+全车轮 全车厢+全车轮 单车厢+单车轮
             */
            //双单情况
            if ((!carriageNum.equals("all")) && (!wheelNum.equals(0))) {
                criteria.andWheelNumEqualTo(wheelNum);
                criteria.andCarriageNumEqualTo(carriageNum);
                //列车唯一编号
                criteria.andTrainOnlyidIn(trainOnlyIdlist);
                vtInfoExample.setOrderByClause("id DESC");

                PageHelper.startPage(page, pageSize);
                vtInfos = vtInfoMapper.selectByExample(vtInfoExample);
                PageInfo<VtInfo> pageInfo = new PageInfo<>(vtInfos);
                //分页返回数据
                PageUtils.pageUtil(pageInfo, pageResultVo);
            }
            //车厢全+车轮单
            if ((carriageNum.equals("all")) && (!wheelNum.equals(0))) {
                List<String> carriagelist = Arrays.asList(Const.CARRIAGES);
                criteria.andWheelNumEqualTo(wheelNum);
                criteria.andCarriageNumIn(carriagelist);
                //列车唯一编号
                criteria.andTrainOnlyidIn(trainOnlyIdlist);
                vtInfoExample.setOrderByClause("id DESC");

                PageHelper.startPage(page, pageSize);
                vtInfos = vtInfoMapper.selectByExample(vtInfoExample);
                PageInfo<VtInfo> pageInfo = new PageInfo<>(vtInfos);
                //分页返回数据
                PageUtils.pageUtil(pageInfo, pageResultVo);
            }
            //单车厢+全车轮
            if ((!carriageNum.equals("all")) && (wheelNum.equals(0))) {
                List<Integer> wheellist = Arrays.asList(Const.WHEELS);
                criteria.andWheelNumIn(wheellist);
                criteria.andCarriageNumEqualTo(carriageNum);
                //列车唯一编号
                criteria.andTrainOnlyidIn(trainOnlyIdlist);
                vtInfoExample.setOrderByClause("id DESC");

                PageHelper.startPage(page, pageSize);
                vtInfos = vtInfoMapper.selectByExample(vtInfoExample);
                PageInfo<VtInfo> pageInfo = new PageInfo<>(vtInfos);
                //分页返回数据
                PageUtils.pageUtil(pageInfo, pageResultVo);
            }
            //双全的情况
            if ((carriageNum.equals("all")) && (wheelNum.equals(0))) {
                List<String> carriagelist = Arrays.asList(Const.CARRIAGES);
                List<Integer> wheellist = Arrays.asList(Const.WHEELS);
                criteria.andWheelNumIn(wheellist);
                criteria.andCarriageNumIn(carriagelist);
                //列车唯一编号
                criteria.andTrainOnlyidIn(trainOnlyIdlist);
                vtInfoExample.setOrderByClause("id DESC");

                PageHelper.startPage(page, pageSize);
                vtInfos = vtInfoMapper.selectByExample(vtInfoExample);
                PageInfo<VtInfo> pageInfo = new PageInfo<>(vtInfos);
                //分页返回数据
                PageUtils.pageUtil(pageInfo, pageResultVo);
            }

            if (vtInfos.size() == 0) {
                return ServerRespone.createByErrorMessage("无数据");
            }

            if (selectType.equals(SelectType.ZHOUWEN.getCode())) {
                for (VtInfo vtInfo : vtInfos) {
                    HistoryReportVO historyReportVO = new HistoryReportVO();
                    historyReportVO.setTrainDate(DateUtil.Date2String(vtInfo.getTrainDate()));
                    historyReportVO.setCarriageNum(vtInfo.getCarriageNum());
                    historyReportVO.setWheelNum(vtInfo.getWheelNum());
                    historyReportVO.setZxTemp(vtInfo.getZxTemp());
                    TrainInfo trainInfo = trainInfoMapper.selectByPrimaryKey(vtInfo.getTrainOnlyid());
                    historyReportVO.setTrainId(trainInfo.getTrainId());
                    historyReportVO.setZxTempinc(vtInfo.getZxTempinc());

                    //需要添加颜色
                    historyReportVO.setZxState(CompareUtil.compare(1, trainParam.getZwWarn().doubleValue(), trainParam.getZwAlarm()
                            .doubleValue(), vtInfo.getZxTemp().doubleValue()));
                    historyReportVO.setZxTempIncState(CompareUtil.compare(1, trainParam.getZwincWarn().doubleValue(), trainParam.getZwincAlarm()
                            .doubleValue(), vtInfo.getZxTempinc().doubleValue()));
                   historyReportVOS.add(historyReportVO);
                }
               pageResultVo.setList(historyReportVOS);//温度
            }else {
                for (VtInfo vtInfo : vtInfos) {
                    HistoryReportVO historyReportVO = new HistoryReportVO();
                    BeanUtils.copyProperties(vtInfo, historyReportVO);
                    historyReportVO.setTrainDate(DateUtil.Date2String(vtInfo.getTrainDate()));
                    historyReportVO.setZxTemp(null);//不设置轴温以及轴温的温升
                    historyReportVO.setZxTempinc(null);
                    TrainInfo trainInfo = trainInfoMapper.selectByPrimaryKey(vtInfo.getTrainOnlyid());
                    historyReportVO.setTrainId(trainInfo.getTrainId());
                    //均方根
                    historyReportVO.setJfgState(CompareUtil.compare(1, trainParam.getJfgWarn().doubleValue()
                            , trainParam.getJfgAlarm().doubleValue(), vtInfo.getJfgValue().doubleValue()));

                    //峭度
                    historyReportVO.setQdState(CompareUtil.compare(1, trainParam.getQdWarn().doubleValue()
                            , trainParam.getQdAlarm().doubleValue(), vtInfo.getQdValue().doubleValue()));

                    //峭度因子
                    historyReportVO.setQdyzState(CompareUtil.compare(1, trainParam.getQdyzWarn().doubleValue()
                            , trainParam.getQdyzAlarm().doubleValue(), vtInfo.getQdyzValue().doubleValue()));

                    //峰值
                    historyReportVO.setFzState(CompareUtil.compare(1, trainParam.getFzWarn().doubleValue()
                            , trainParam.getFzyzAlarm().doubleValue(), vtInfo.getFzValue().doubleValue()));

                    //峰值因子
                    historyReportVO.setFzyzState(CompareUtil.compare(1, trainParam.getFzyzWarn().doubleValue()
                            , trainParam.getFzyzAlarm().doubleValue(), vtInfo.getFzyzValue().doubleValue()));

                    historyReportVOS.add(historyReportVO);
                }
                pageResultVo.setList(historyReportVOS);
            }
            return ServerRespone.createBySuccess(pageResultVo);
        } else {
            MotorInfoExample motorInfoExample = new MotorInfoExample();
            MotorInfoExample.Criteria criteria = motorInfoExample.createCriteria();

            List<MotorInfo> motorInfoList = new ArrayList<>();
            PageResultVo pageResultVo = new PageResultVo();
            //单车厢+单电机
            if ((!carriageNum.equals("all")) && (!motorNum.equals(0))) {
                criteria.andCarriageNumEqualTo(carriageNum);
                criteria.andMotorNumEqualTo(motorNum);
                //列车唯一编号
                criteria.andTrainOnlyidIn(trainOnlyIdlist);
                motorInfoExample.setOrderByClause("id DESC");
                //查询出所有的数据
                PageHelper.startPage(page, pageSize);
                motorInfoList = motorInfoMapper.selectByExample(motorInfoExample);
                PageInfo<MotorInfo> pageInfo = new PageInfo<>(motorInfoList);
                PageUtils.pageUtil(pageInfo, pageResultVo);
            }
            //全车厢+单电机
            if ((carriageNum.equals("all")) && (!motorNum.equals(0))) {
                List<String> carriagelist = Arrays.asList(Const.CARRIAGES);
                criteria.andCarriageNumIn(carriagelist);
                criteria.andMotorNumEqualTo(motorNum);
                //列车唯一编号
                criteria.andTrainOnlyidIn(trainOnlyIdlist);
                motorInfoExample.setOrderByClause("id DESC");
                //查询出所有的数据
                PageHelper.startPage(page, pageSize);
                motorInfoList = motorInfoMapper.selectByExample(motorInfoExample);
                PageInfo<MotorInfo> pageInfo = new PageInfo<>(motorInfoList);
                PageUtils.pageUtil(pageInfo, pageResultVo);
            }
            //单车厢+全电机
            if ((!carriageNum.equals("all")) && (motorNum.equals(0))) {
                List<Integer> motorList = Arrays.asList(Const.MOTORNUM);
                criteria.andCarriageNumEqualTo(carriageNum);
                criteria.andMotorNumIn(motorList);
                //列车唯一编号
                criteria.andTrainOnlyidIn(trainOnlyIdlist);
                motorInfoExample.setOrderByClause("id DESC");
                //查询出所有的数据
                PageHelper.startPage(page, pageSize);
                motorInfoList = motorInfoMapper.selectByExample(motorInfoExample);
                PageInfo<MotorInfo> pageInfo = new PageInfo<>(motorInfoList);
                PageUtils.pageUtil(pageInfo, pageResultVo);
            }
            //双全
            if ((carriageNum.equals("all")) && (motorNum.equals(0))) {
                List<String> carriagelist = Arrays.asList(Const.CARRIAGES);
                List<Integer> motorList = Arrays.asList(Const.MOTORNUM);
                criteria.andCarriageNumIn(carriagelist);
                criteria.andMotorNumIn(motorList);
                //列车唯一编号
                criteria.andTrainOnlyidIn(trainOnlyIdlist);
                motorInfoExample.setOrderByClause("id DESC");
                //查询出所有的数据
                PageHelper.startPage(page, pageSize);
                motorInfoList = motorInfoMapper.selectByExample(motorInfoExample);
                PageInfo<MotorInfo> pageInfo = new PageInfo<>(motorInfoList);
                PageUtils.pageUtil(pageInfo, pageResultVo);
            }

            if (motorInfoList.size() == 0) {
                return ServerRespone.createByErrorMessage("无数据");
            }



                //表明为电机温度的查询
                for (MotorInfo motorInfo : motorInfoList) {
                    HistoryReportVO historyReportVO = new HistoryReportVO();
                    BeanUtils.copyProperties(motorInfo, historyReportVO);
                    TrainInfo trainInfo = trainInfoMapper.selectByPrimaryKey(motorInfo.getTrainOnlyid());
                    historyReportVO.setTrainId(trainInfo.getTrainId());
                    historyReportVO.setTrainDate(DateUtil.Date2String(motorInfo.getTrainDate()));
                    historyReportVO.setMotorTempState(CompareUtil.compare(1, trainParam.getMotortempWarn().doubleValue()
                            , trainParam.getMotortempAlarm().doubleValue(), motorInfo.getMotorTemp().doubleValue()));
                    historyReportVO.setMotorTempIncState(CompareUtil.compare(1, trainParam.getMotortempincWarn().doubleValue()
                            , trainParam.getMotortempincAlarm().doubleValue(), motorInfo.getMotorTempinc().doubleValue()));
                    historyReportVOS.add(historyReportVO);
                }
                pageResultVo.setList(historyReportVOS);
                return ServerRespone.createBySuccess(pageResultVo);

        }

    }
    /**
     * 新添加的进行当天过车的车辆进行最值及平均值得显示
     * 由于时间紧此处以后需要优化,写的是真的垃圾
     *
     * @param firsttime
     * @param lasttime
     * @return
     */


    @Override
    public ServerRespone getOptiAndAvg(Date firsttime, Date lasttime) {
        //获取所有的列车号，进行数据的查询
        List<String> tainIdList = Arrays.asList(Const.TRAINID);
        List<OptiAndAvgVO> optiAndAvgVOS = new ArrayList<>();
        //获取所有的trianOnlyId
        for (String tranId : tainIdList) {
            List<Long> trainOnlyIdList = trainInfoMapper.findTrainOnlyIdBytrainIdAndtrainDate(tranId, firsttime, lasttime);
            //获取当前时间的该列车的所有trainOnlyId
            if (trainOnlyIdList.size() == 0) {
                continue;
            }

            OptiAndAvgVO optiAndAvgVO = vtInfoMapper.findfzValue1(trainOnlyIdList);
            optiAndAvgVO.setTrainId(tranId);
            optiAndAvgVOS.add(optiAndAvgVO);
        }
        if (optiAndAvgVOS.size() == 0) {
            return ServerRespone.createByErrorMessage("无数据");
        }
        return ServerRespone.createBySuccess(optiAndAvgVOS);
    }



}





